// ToolBox.cpp: implementation of the CToolBoxBar class.
//

#include "stdafx.h"
#include "ToolBox.h"
#include "Resource.h"
#include "EditorGuidDef.h"
#include "MessageGeomUI.h"
#include "GeomApp.h"
#include "EditorConfig.h"
#include "PropFactory.h"
#include "EditorData.h"
#include "GeomDocTree.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


#define PAGE_CAMERA		0
#define PAGE_STDVIEW	1
#define PAGE_SKETCH		2
#define PAGE_FEATURE	3
#define PAGE_PRIMITIVE	4

//////////////////////////////////////////////////////////////////////////
class CEditorItemVisitor : public ICatalogVisitor
{
public:
	CEditorItemVisitor(CToolBoxBarEx* pToolBox, CBCGPToolBoxPage* pPage, UINT nPage)
		:m_pToolBox(pToolBox),
		m_pPage(pPage),
		m_nPage(nPage)
	{

	}

	virtual void OnVisitItem(EditorItem* pItem)
	{
		if(pItem->hIcon == NULL)
			pItem->hIcon = m_pToolBox->DefaultIcon();
		UINT idButton = m_pPage->AddButton(pItem->szName, pItem->hIcon);
		m_pToolBox->SetCommandMap(MAKELONG(m_nPage, idButton), pItem->guid);
		GetPropFactory().AddPropScript(pItem->guid, pItem->szProperty);

		CGeomDocTree::AddTreeItemIcon(MAKELONG(m_nPage, idButton), pItem->hIcon);
	}
private:
	CToolBoxBarEx* m_pToolBox;
	CBCGPToolBoxPage* m_pPage;
	UINT m_nPage;
};


/////////////////////////////////////////////////////////////////////////////
// CToolBoxBarEx

CToolBoxBarEx::CToolBoxBarEx()
{
	m_hIconDefault = NULL;
}

CToolBoxBarEx::~CToolBoxBarEx()
{
}

BEGIN_MESSAGE_MAP(CToolBoxBarEx, CBCGPToolBoxEx)
	//{{AFX_MSG_MAP(CToolBoxBarEx)
	//}}AFX_MSG_MAP
	ON_WM_CREATE()
END_MESSAGE_MAP()

void CToolBoxBarEx::SetCommandMap(UINT nID, const CString& guid)
{
	m_mapCommand[nID] = guid;
}

int CToolBoxBarEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CBCGPToolBoxEx::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	CImageList imageList;
	imageList.Create(IDB_BITMAP_DEFAULT,16,2,RGB(0,255,0));
	m_hIconDefault = imageList.ExtractIcon(0);

	// TODO:  Add your specialized creation code here
	AddToolsPage(_T("Camera"), IDB_BITMAP_CAMERA,16,
		_T("Previous\nMove\nRotate\nZoom\nFit\nFit Area\nFit Selected"));
	AddToolsPage(_T("Standrad View"), IDB_BITMAP_STDCAMERA,16,
		_T("Front\nBack\nLeft\nRight\nTop\nBottom\nIsometric"));

	AddToolsPage(_T("Sketch"), IDB_BITMAP_POINTER,16,
		_T("Modify"));
	AddToolsPage(_T("Feature"), IDB_BITMAP_POINTER,16,
		_T("Modify"));
	AddToolsPage(_T("Primitive"), IDB_BITMAP_POINTER,16,
		_T("Modify"));

	SetActivePage (0);
	GetPage(PAGE_CAMERA)->SetMode(CBCGPToolBoxPage::ToolBoxPageMode_Images);
	GetPage(PAGE_STDVIEW)->SetMode(CBCGPToolBoxPage::ToolBoxPageMode_Images);
	GetPage(PAGE_SKETCH)->SetMode(CBCGPToolBoxPage::ToolBoxPageMode_Images);
	//GetPage(PAGE_FEATURE)->SetMode(CBCGPToolBoxPage::ToolBoxPageMode_Images);
	GetPage(PAGE_PRIMITIVE)->SetMode(CBCGPToolBoxPage::ToolBoxPageMode_Images);
	CGeomDocTree::InitTreeIcons(&imageList);
	//CGeomDocTree::AddTreeItemIcon(0,m_hIconDefault);
	InitCommandMap();
	GetGeomApp().GetDocTree()->ApplyIconsToTree();
	return 0;
}

void CToolBoxBarEx::OnToolSelected()
{
	CView* pView = GetActiveView();
	if(NULL == pView)
		return;

	int nPage = -1;
	int nTool = -1;

	nTool = GetLastClickedTool (nPage);
	CBCGPToolBoxPage* pPage = GetPage (nPage);
	CBCGPToolBoxButton* pTool = pPage->GetButton (nTool);
	pTool->SetCheck(FALSE);
	HICON hIcon = pPage->GetImageList().ExtractIcon(pTool->GetImageIndex());
	CEditorData edData;
	edData.nID = MAKELONG(nPage, nTool);
	edData.hIcon = hIcon;

	CString szCmd = IdToString(nPage, nTool);
	::SendMessage(pView->GetSafeHwnd(), WM_GEOM_STARTEDITOR, 
		(WPARAM)&szCmd, (LPARAM)&edData);

	//::SendMessage(AfxGetMainWnd()->GetSafeHwnd(),
	//	WM_GEOM_HELP_COMMOND, GHM_NAVIGATE_NEW, 0);
}

void CToolBoxBarEx::InitCommandMap()
{
	//
	m_mapCommand[MAKELONG(PAGE_CAMERA,1)] = EGUID_CAMERA_PAN;
	m_mapCommand[MAKELONG(PAGE_CAMERA,2)] = EGUID_CAMERA_ROTATE;
	m_mapCommand[MAKELONG(PAGE_CAMERA,3)] = EGUID_CAMERA_FREEZOOM;
	
	m_mapCommand[MAKELONG(PAGE_STDVIEW,0)] = EGUID_VIEW_FRONT;
	m_mapCommand[MAKELONG(PAGE_STDVIEW,1)] = EGUID_VIEW_BACK;
	m_mapCommand[MAKELONG(PAGE_STDVIEW,2)] = EGUID_VIEW_LEFT;
	m_mapCommand[MAKELONG(PAGE_STDVIEW,3)] = EGUID_VIEW_RIGHT;
	m_mapCommand[MAKELONG(PAGE_STDVIEW,4)] = EGUID_VIEW_TOP;
	m_mapCommand[MAKELONG(PAGE_STDVIEW,5)] = EGUID_VIEW_BOTTOM;
	m_mapCommand[MAKELONG(PAGE_STDVIEW,6)] = EGUID_VIEW_ISOMETRIC;

	m_mapCommand[MAKELONG(PAGE_SKETCH,0)] = EGUID_SELECT;
	m_mapCommand[MAKELONG(PAGE_FEATURE,0)] = EGUID_SELECT;
	m_mapCommand[MAKELONG(PAGE_PRIMITIVE,0)] = EGUID_SELECT;

	CBCGPToolBoxPage* pSketchPage = GetPage(PAGE_SKETCH);
	if(pSketchPage)
	{
		CEditorItemVisitor sketchVisitor(this, pSketchPage, PAGE_SKETCH);
		GetEditorConfig().VisitCatalog(CEditorConfig::EC_SKETCH, &sketchVisitor);
	}

	CBCGPToolBoxPage* pPrimitivePage = GetPage(PAGE_PRIMITIVE);
	if(pPrimitivePage)
	{
		CEditorItemVisitor visitor(this, pPrimitivePage, PAGE_PRIMITIVE);
		GetEditorConfig().VisitCatalog(CEditorConfig::EC_PRIMIVE, &visitor);
	}

	CBCGPToolBoxPage* pFeaturePage = GetPage(PAGE_FEATURE);
	if(pFeaturePage)
	{
		CEditorItemVisitor visitor(this, pFeaturePage, PAGE_FEATURE);
		GetEditorConfig().VisitCatalog(CEditorConfig::EC_FEATURE, &visitor);
	}
}

CString CToolBoxBarEx::IdToString(int nPage, int nTool)
{
	static CString szCmd;
	BOOL bFind = m_mapCommand.Lookup(MAKELONG(nPage, nTool), szCmd);

	return bFind ? szCmd : _T("");
}